package com.zx.cloud.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zx.cloud.domain.Dict;
import com.zx.cloud.domain.VerifyEntity;
import com.zx.cloud.persistence.DictMapper;
import com.zx.cloud.util.HttpUtils;
/**
 * 校验ip、设备黑名单
 */
@Service
public class VerifyEquipmentService {
	 private static final Logger LOGGER = LoggerFactory.getLogger(VerifyEquipmentService.class);
	@Autowired
	private DictMapper dictMapper;
	
	/**
	 * 校验ip、设备黑名单
	 */
	public int verifyEquipment(VerifyEntity verifyEntity){
  		int resultInt = 1;
  		String url_step3 = null;
  		String url_step4 = null;
  		Dict dict = new Dict();
		dict.setParentCode("PRELOAN");
		List<Dict> dicts = dictMapper.getDitByParentCode(dict);
		if(null != dicts && dicts.size() > 0){
			for(Dict tmp : dicts){
				if("url_step3".equals(tmp.getName())){
					url_step3 = tmp.getValue();
				}
				if("url_step4".equals(tmp.getName())){
					url_step4 = tmp.getValue();
				}
			}
		}
		String body = "name=" + verifyEntity.getName() + "&id_number="+ verifyEntity.getId_number()+ "&mobile=" + verifyEntity.getMobile() 
				+ "&ip_address=" + verifyEntity.getIpAddress() + "&black_box="+ verifyEntity.getBlackBox()+ "";
		String response=null;
//		try {
//			response = HttpUtils.sentPost(url_step3, body);
//		} catch (Exception e) {
//			resultInt=2;
//			return resultInt;
//		}
//
//  		if(StringUtils.isNotEmpty(response)){
//  			LOGGER.info("======校验ip、设备黑名单=====风险决策接口==========response:"+response);
//  			JSONObject json = new JSONObject(response);
//  			Boolean success = json.getBoolean("success");
//  			if(success){
//  				String seq_id = json.getString("seq_id");
//  				LOGGER.info("======校验ip、设备黑名单=====获取seq_id==========seq_id:"+seq_id);
//  				JSONArray hitRules = json.getJSONArray("hit_rules");
//  				if(hitRules.length() > 0){
//  					resultInt=2;
//  				}else{
//  					resultInt=1;
//  				}
//  			}
//  		}
  		return resultInt;
  	}
  	/**
  	 * 解析report_id
  	 */
  	public int queryVerifyResult(String url, String seq_id){
  		int result = 1;
  		int score=0;
  		url = url +"&sequence_id="+seq_id;
  		String response = HttpUtils.doGet(url, null, 3, 10, "UTF-8");
  		if(StringUtils.isNotEmpty(response)){
  			LOGGER.info("======校验ip、设备黑名单=====解析seq_id==========response:"+response);
  			if(response.contains("rules")){ 
  				JSONObject json = new JSONObject(response);
  				JSONArray rules = json.getJSONArray("rules");
  				for (int i = 0; i < rules.length(); i++) {
  					JSONObject rule =(JSONObject) rules.get(i);
  					JSONArray  conditions=rule.getJSONArray("conditions");
  					for (int j = 0; j < conditions.length(); j++) {
  						JSONObject condition =conditions.getJSONObject(j);
  						LOGGER.info("======校验ip、设备黑名单=====解析condition==========condition:"+condition);
  						
  						if(condition.has("dim_type")){
  							String dim_type=(String)condition.get("dim_type");
  							if("ip_address".equals(dim_type)){
  								score=Integer.parseInt(rule.get("score").toString());
  								LOGGER.info("======校验ip、设备黑名单=====解析dim_type==========score:"+score);
  								result=2;
  								break;
  							}
  						}
					}
  					
  					
				}
  				
  			}
  		}
  		LOGGER.info("======校验ip、设备黑名单=====解析seq_id==========score:"+score);
  		return result;
  	   
    }
}